﻿//-----------------------------------------------------------------------
// <copyright file="Cart.cs" company="上海Zoomla!逐浪CMS团队">
// 版权所有:🏢江西逐浪软件科技有限公司  🏅🏅🏅20年专注研发==>商城|ERP|字库|高端门户与人工智能 
// Website:🚀www.z01.com 
// Contact:☎ 13177777714  😎 wechat: zoomla 
// Effect: 🔧 购物车实体  
// Create: ⏰2020年09月16日,08:06
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SportsStore.Domain.Entities
{
    public class Cart
    {
        private List<CartLine> lineCollection = new List<CartLine>();


        /// <summary>
        /// 加入购物车
        /// </summary>
        /// <param name="product"></param>
        /// <param name="quantity"></param>
        public void AddItem(Product product,int quantity)
        {
            CartLine line = lineCollection
            .Where(p => p.Product.ProductID == product.ProductID)
            .FirstOrDefault();

            if (line==null)
            {
                lineCollection.Add(new CartLine { Product = product, Quantity = quantity });
            }
            else
            {
                line.Quantity += quantity;
            }
        }
        
        /// <summary>
        /// 移除购物车商品
        /// </summary>
        public void RemoveLine(Product product)
        {
            lineCollection.RemoveAll(l => l.Product.ProductID == product.ProductID);
        }

        /// <summary>
        /// 购物车统计
        /// </summary>
        /// <returns></returns>
        public decimal ComputeTotalValue()
        {
            return lineCollection.Sum(e => e.Product.Price * e.Quantity);
        }

        /// <summary>
        /// 清空购物车
        /// </summary>
        public void Clear()
        {
            lineCollection.Clear();
        }

        public IEnumerable<CartLine> lines {
        get { return lineCollection; }
        }

        public class CartLine
        {
            public Product Product { get; set; }
            public int Quantity { get; set; }
        }

    }
}
